﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.BusinessLogic;
using Module.Core.BusinessLogic.Advertisement;
using Module.Core.Common;
using Module.Core.EntitiesInfo;
using Module.Core.LoginSystem.Commons.Framwork;

namespace Web.Cms.Advertistment
{
    public partial class AddEditAdvPostion : AdminPage
    {
        private int advPostion = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                advPostion = Convert.ToInt32(Request.QueryString["PositionID"]);
                if (!IsPostBack)
                {
                    BannerPos.Fill(dropPositions);
                    AdvertisementPositionsInfo advertisementPositionsInfo = AdvertisementPositionsBL.Select(advPostion);
                    if (advertisementPositionsInfo != null)
                    {
                        txtName.Text = advertisementPositionsInfo.Title;
                        txtDescription.Text = advertisementPositionsInfo.Description;
                        rdoShowMode.SelectedValue = advertisementPositionsInfo.ShowMode.ToString();
                        txtWidth.Text = advertisementPositionsInfo.Width.ToString();
                        txtHeight.Text = advertisementPositionsInfo.Height.ToString();
                        txtTimeDelay.Text = advertisementPositionsInfo.TimeDelay.ToString();
                        dropPositions.SelectedValue = advertisementPositionsInfo.GroupPostion.ToString();
                        btnUpdate.Visible = true;
                        btnAdd.Visible = false;
                    }
                    else
                    {
                        btnUpdate.Visible = false;
                        btnAdd.Visible = true;
                    }
                }
            }
            catch (Exception ex)
            {

                lblUpdateStatus.Text = "Lỗi" + ex.Message;
            }
        }
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                AdvertisementPositionsInfo adv = new AdvertisementPositionsInfo();
                FillData(adv);
                AdvertisementPositionsBL.Update(adv);
                lblUpdateStatus.Text = "Bạn đã chỉnh sửa thành công";
            }
            catch (Exception ex)
            {

                lblUpdateStatus.Text = "Lỗi" + ex.Message;
            }

        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AdvertisementPositionsInfo adv = new AdvertisementPositionsInfo();
                FillData(adv);
                AdvertisementPositionsBL.Insert(adv);
                lblUpdateStatus.Text = "Bạn đã thêm mới thành công";
            }
            catch (Exception ex)
            {
                lblUpdateStatus.Text = "Lỗi" + ex.Message;
            }
        }
        void FillData(AdvertisementPositionsInfo advertisementPositionsInfo)
        {
            advertisementPositionsInfo.PositionID = advPostion;
            advertisementPositionsInfo.Title = txtName.Text;
            advertisementPositionsInfo.Description = txtDescription.Text;
            advertisementPositionsInfo.ShowMode = ObjConvertUtility.ToInt32(rdoShowMode.SelectedValue);
            advertisementPositionsInfo.RepeatColumns = 1;
            advertisementPositionsInfo.CreatedBy = CurrentUser.UserID;
            advertisementPositionsInfo.CreatedOn = DateTime.Now;
            advertisementPositionsInfo.Width = ObjConvertUtility.ToInt32(txtWidth.Text);
            advertisementPositionsInfo.Height = ObjConvertUtility.ToInt32(txtHeight.Text);
            advertisementPositionsInfo.TimeDelay = float.Parse(txtTimeDelay.Text);
            advertisementPositionsInfo.Priority = 0;
            advertisementPositionsInfo.Lang = 1;
            advertisementPositionsInfo.GroupPostion = Convert.ToInt32(dropPositions.SelectedValue);
        }
    }
}